import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import matplotlib


matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False


# === 1. WHO 男婴 体重标准（每月龄） ===
who_data = {
    "AgeDays": [0, 30, 60, 90, 120, 150, 180],
    "-3SD":    [2.0, 3.4, 4.4, 5.1, 5.7, 6.1, 6.5],
    "-2SD":    [2.5, 4.0, 5.0, 5.8, 6.4, 6.9, 7.3],
    "-1SD":    [2.9, 4.5, 5.6, 6.4, 7.1, 7.6, 8.0],
    "Median":  [3.3, 5.0, 6.1, 7.0, 7.8, 8.3, 8.7],
    "+1SD":    [3.9, 5.8, 6.9, 7.9, 8.7, 9.3, 9.7],
    "+2SD":    [4.4, 6.4, 7.6, 8.6, 9.4, 10.0, 10.5],
    "+3SD":    [5.0, 7.0, 8.3, 9.4, 10.2, 10.9, 11.4],
}
who_df = pd.DataFrame(who_data)

# === 2. 插值，生成每天一条线（0~180天） ===
x_fine = np.arange(0, 181, 1)
curves = {}
for col in who_df.columns[1:]:
    interp = interp1d(who_df["AgeDays"], who_df[col], kind='cubic')
    curves[col] = interp(x_fine)

# === 3. Linlin 体重数据，从CSV读取 ===
# 可以保存为 linlin.csv 文件
csv_data = """
SubjectID,Sex,AgeDays,Weight,MeasureDate
Linlin,1,0,3.76,2025/4/30
Linlin,1,5,3.78,2025/5/4
Linlin,1,6,3.75,2025/5/5
Linlin,1,7,3.8,2025/5/6
Linlin,1,8,3.9,2025/5/7
Linlin,1,9,3.94,2025/5/8
Linlin,1,10,4,2025/5/9
Linlin,1,11,4.4,2025/5/10
Linlin,1,12,4.1,2025/5/11
Linlin,1,13,4.15,2025/5/12
Linlin,1,14,4.2,2025/5/13
Linlin,1,15,4.25,2025/5/14
Linlin,1,16,4.32,2025/5/15
Linlin,1,17,4.4,2025/5/16
Linlin,1,18,4.51,2025/5/17
Linlin,1,19,4.57,2025/5/18
Linlin,1,20,4.61,2025/5/19
Linlin,1,21,4.66,2025/5/20
Linlin,1,22,4.72,2025/5/21
Linlin,1,23,4.78,2025/5/22
Linlin,1,24,4.86,2025/5/23
Linlin,1,25,4.94,2025/5/24
Linlin,1,26,5.01,2025/5/25
Linlin,1,27,5.08,2025/5/26
Linlin,1,28,5.15,2025/5/27
Linlin,1,29,5.2,2025/5/28
Linlin,1,30,5.26,2025/5/29
Linlin,1,31,5.9,2025/5/30
Linlin,1,32,5.35,2025/5/31
Linlin,1,33,5.41,2025/6/1
Linlin,1,34,5.45,2025/6/2
Linlin,1,36,5.52,2025/6/4
Linlin,1,37,5.59,2025/6/5
Linlin,1,38,5.63,2025/6/6
Linlin,1,39,5.6,2025/6/7
Linlin,1,40,5.62,2025/6/8
Linlin,1,41,5.68,2025/6/9
Linlin,1,42,5.71,2025/6/10
Linlin,1,49,5.82,2025/6/17
Linlin,1,52,5.93,2025/6/20
Linlin,1,54,5.95,2025/6/22
Linlin,1,59,5.99,2025/6/27
Linlin,1,60,6.04,2025/6/28
Linlin,1,61,6.12,2025/6/29
Linlin,1,64,6.16,2025/7/2
Linlin,1,67,6.16,2025/7/5
Linlin,1,68,6.24,2025/7/6
Linlin,1,70,6.35,2025/7/8
Linlin,1,73,6.37,2025/7/11
Linlin,1,76,6.48,2025/7/14
Linlin,1,78,6.52,2025/7/16
Linlin,1,81,6.55,2025/7/19
Linlin,1,82,6.57,2025/7/20
"""
from io import StringIO
linlin_df = pd.read_csv(StringIO(csv_data))

# === 4. 绘图 ===
plt.figure(figsize=(12, 6))
plt.title("霖霖 vs WHO 男婴体重标准曲线 (0-180天)", fontsize=14)

# 绘制 WHO 曲线
for label, style in zip(["-3SD", "-2SD", "-1SD", "Median", "+1SD", "+2SD", "+3SD"],
                        ['--', '--', '--', '-', '--', '--', '--']):
    plt.plot(x_fine, curves[label], linestyle=style, label=label)

# 绘制 Linlin 的数据点
plt.plot(linlin_df["AgeDays"], linlin_df["Weight"], 'ro-', label="Linlin", linewidth=2, markersize=5)

plt.xlabel("天龄 (Day)")
plt.ylabel("体重 (kg)")
plt.grid(True, linestyle="--", alpha=0.5)
plt.legend()
plt.tight_layout()
plt.show()
